#!/usr/bin/env python
#
'''
Created on 2009-4-5

@author: Wang_Yuan
'''

import os
import wx

from WordDetailPanel_xrc import *


class WordDetailPanel(xrcWordDetailPanel):
    '''
    Word detail panel. 
    '''
    
    def __init__(self, parent, wordData):
        xrcWordDetailPanel.__init__(self, parent)
        self.setWordData(wordData)
        
    def setWordData(self, wordData):
        if wordData:
            self.wordData = wordData
            self.titlePanel.SetLabel(wordData.word)
            self.wordLabel.SetLabel(wordData.word)
            self.pronunciationLabel.SetLabel(wordData.pronunciation)
            self.meaningText.ChangeValue(wordData.getMeaningStr())
            self.exampleText.ChangeValue(
                             os.linesep.join(
                                             map(
                                                 lambda example: os.linesep.join([example.originalPart(), example.translation, '']), 
                                                 wordData.getExampleList()
                                                 )
                                             )
                                         )
    
    
class WordDetailFrame(wx.MiniFrame):
    
    def __init__(self, parent, wordData, pos=wx.DefaultPosition, size=wx.DefaultSize):
        wx.MiniFrame.__init__(self, parent, wx.ID_ANY, title=wordData.word, pos=pos, size=size, style=wx.NO_3D)
        self.panel = WordDetailPanel(self, wordData)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel)
        sizer.Fit(self)
        self.delta = (0,0)
        self.__initEvents()
        
    def __initEvents(self):
        self.moveBar = self.panel.titlePanel
        self.moveBar.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.moveBar.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
        self.moveBar.Bind(wx.EVT_MOTION, self.OnMouseMove)
        self.panel.closeIcon.Bind(wx.EVT_LEFT_DOWN, self.OnClose)
        
    def OnLeftDown(self, evt):
        self.moveBar.CaptureMouse()
        x, y = self.moveBar.ClientToScreen(evt.GetPosition())
        originx, originy = self.GetPosition()
        dx = x - originx
        dy = y - originy
        self.delta = ((dx, dy))


    def OnLeftUp(self, evt):
        if self.moveBar.HasCapture():
            self.moveBar.ReleaseMouse()


    def OnMouseMove(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            x, y = self.moveBar.ClientToScreen(evt.GetPosition())
            fp = (x - self.delta[0], y - self.delta[1])
            self.Move(fp)
            
    def OnClose(self, evt):
        self.Destroy()
    